package com.hjq.fcloud.utils

import android.text.TextUtils
import com.alibaba.fastjson.JSON
import com.hjq.fcloud.bean.ProjectItemBean
import com.hjq.fcloud.config.Config
import com.hjq.fcloud.http.api.LoginApi
import com.tencent.mmkv.MMKV
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

object AppConfigUtils {

    /**
     * 保存当前需求对象
     */
    fun saveSelectProjectItemBean(item: ProjectItemBean?) {
        if(item != null){
            MMKV.defaultMMKV().encode(Config.SP_SELECT_FNC, JSON.toJSONString(item))
        }
    }

    /**
     * 获取当前需求对象
     */
    fun getSelectProjectItemBean():ProjectItemBean? {
        var fncJsonStr = MMKV.defaultMMKV().decodeString(Config.SP_SELECT_FNC)
        if(!TextUtils.isEmpty(fncJsonStr)){
            return JSON.parseObject(fncJsonStr,ProjectItemBean::class.java)
        }
        return null
    }
    fun saveToken(token: String?) {
        MMKV.defaultMMKV().encode(Config.SP_USER_TOKEN, token)
    }

    fun savePhone(phone: String?) {
        MMKV.defaultMMKV().encode(Config.SP_USER_PHONE, phone)
    }

    fun saveLoginData(data: LoginApi.DataJson?) {
        data?.let {
            MMKV.defaultMMKV().encode(Config.SP_USER_LOGIN_JSON, JSON.toJSONString(data))
        }
    }

    fun getLoginData():LoginApi.DataJson? {
        try {
            var json = MMKV.defaultMMKV().decodeString(Config.SP_USER_LOGIN_JSON)
            return JSON.parseObject(json,LoginApi.DataJson::class.java)
        }catch (e:Exception){
            e.printStackTrace()
            return null
        }
    }

    val phone: String?
        get() = MMKV.defaultMMKV().decodeString(Config.SP_USER_PHONE)

    @JvmStatic
    val token: String?
        get() = MMKV.defaultMMKV().decodeString(Config.SP_USER_TOKEN, "")

    fun saveFaceBase64(base64: String?,callBack: () -> Unit) {
        Observable.create<Boolean> {
            var ret = MMKV.defaultMMKV().encode(Config.SP_FACE_BASE64, base64)
            it.onNext(ret)
        }.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                callBack()
            }
    }

    fun getFaceBase64(callBack: (String?) -> Unit) {
        Observable.create<String> {
            var ret = MMKV.defaultMMKV().decodeString(Config.SP_FACE_BASE64)
            if (ret != null) {
                it.onNext(ret)
            } else {
                it.onNext("")
            }
        }.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                callBack(it)
            }
    }

    fun checkLoginOK(): Boolean {
        val token = token
        return if (!TextUtils.isEmpty(token)) {
            true
        } else false
    }

    fun saveLaunchOk(isOk: Boolean) {
        MMKV.defaultMMKV().encode(Config.MMKV_IS_LAUNCH, isOk)
    }

    val launchOk: Boolean
        get() = MMKV.defaultMMKV().decodeBool(Config.MMKV_IS_LAUNCH, false)

    fun clealAppData() {
        MMKV.defaultMMKV().encode(Config.SP_USER_TOKEN, "")
    }
}